<template>
    <el-upload
        class="upload-demo"
        accept=".jpeg,.png,.jpg,.bmp,.gif,.webp"
        :action="uploadUrl"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"                    
        :on-exceed="handleExceed"
        :on-success="handleFileSuccess"
        :headers="headers"
        :file-list="dataList"
        :limit="limit"
        >
        <el-button size="small" type="primary" dis>点击上传</el-button>
        <!-- <div slot="tip" class="el-upload__tip" v-if="tip.length>0">{{ tip }}</div> -->
    </el-upload>
</template>

<script>
    import { getToken } from '@/utils/auth'
    export default{
        name: 'ELUpload',
        props: {
            limit:{
                type: Number,
                default: 1
            },
            fileList:[]
            // tip:{
            //     type: String,
            //     default: '只能上传jpg/png文件，且不超过500kb'
            // }
        },
        data () {
            return {
                dataList:this.fileList
            }
        },
        computed: {
            uploadUrl(){
                return process.env.VUE_APP_BASE_API+'/public/file/upload'
            },
            headers() {
                return { Authorization: getToken() };
            },
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.id == file.id) {
                        fileList.splice(index, 1);
                        break;
                    }
                }
                this.dataList = fileList;
                this.$emit("uploadFileSuccess", fileList);
            },
            handlePreview(file) {
                console.log(file);
                this.$viewerApi({ images: [file.fileUrl] })

                // const viewer = this.$el.querySelector('.images').$viewer
                // viewer.show()
            },
            handleExceed(files, fileList) {
                // this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
                this.$notify.warning({
                    title:'提示',
        //   title: `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`,
          message: `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`,
        });
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
            //点击视频查看
            handleVideoPreview(file){
                this.$refs.coursePlayerDialog.videoUrl = file.url;
                this.$refs.coursePlayerDialog.dialogVisible = true;
                this.$refs.coursePlayerDialog.coursePlayerHandle();
            },
            beforeVideoUpload(file){
                const isLimit = file.size / 1024 / 1024 / 1024 < 1
                const isVideo = /video\/\w+/.test(file.type)
                if (!isLimit) {
                    this.$notify({
                    title: '上传视频大小不能超过 1G!',
                    type: 'warning'
                    })
                }
                if (!isVideo) {
                    this.$notify({
                        title: '只能上传视频文件!',
                        type: 'warning'
                    })
                }
                this.isValidate = isLimit && isVideo
                return isLimit && isVideo
            },
            handleFileSuccess(response, file, fileList) {//封面附件上传成功
                console.log(JSON.stringify(fileList));
                console.log("--------------------------------");
                let fileList2 = [];
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    if (element.raw == null || element.raw == undefined){
                        fileList2.push(element);
                    }else{
                        element.name = element.fileName;
                        element.response.data.name = element.response.data.fileName;
                        element.response.data.cover = '1';//是否封面
                        fileList2.push(element.response.data);
                    }
                }
                console.log(JSON.stringify(fileList2));
                this.dataList = fileList2;
                this.$emit("uploadFileSuccess", fileList2);
            },
            handleVideoSuccess(response, file, fileList){//视频附件上传成功
                let fileList2 = [];
                for (let index = 0; index < fileList.length; index++) {
                    const element = fileList[index];
                    element.name = element.fileName;
                    element.response.data.name = element.response.data.fileName;
                    element.response.data.cover = '0';//是否封面
                    fileList2.push(element.response.data);
                }
                console.log(JSON.stringify(fileList2));
                this.form.videoList = fileList2;
            },
            submitHandle(){
                
                
                
                
            }
        }
    }

</script>